package cn.cloudreal.cbms.common.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import org.apache.log4j.Logger;

public class BaseDAO
{

    private static final Logger log = Logger.getLogger(BaseDAO.class);

    public static Connection getConn()
    {
        // 开始打印日志
        if (log.isInfoEnabled())
        {
            log.info("Enter BaseDAO getConn() method, return String");
        }
        String driver = "";
        String url = "";
        String name = "";
        String pass = "";
        try
        {
            try
            {
                // 创建读取properties配制文件的对象
                Properties prop = new Properties();
                prop.load(BaseDAO.class.getClassLoader().getResourceAsStream("jdbc.properties"));
                driver = prop.getProperty("jdbc.driverClassName");
                url = prop.getProperty("jdbc.url");
                name = prop.getProperty("jdbc.username");
                pass = prop.getProperty("jdbc.password");
            }
            catch (Exception e)
            {
                // TODO: handle exception
                e.printStackTrace();
            }

            // try{
            Class.forName(driver);
            Connection con = DriverManager.getConnection(url, name, pass);
            return con;
        }
        catch (ClassNotFoundException e)
        {
            // TODO 自动生成 catch 块
            e.printStackTrace();
        }
        catch (SQLException e)
        {
            // TODO 自动生成 catch 块
            e.printStackTrace();
        }
     // 开始打印日志
        if (log.isInfoEnabled())
        {
            log.info("Exit BaseDAO getConn() method, return String");
        }
        return null;
    }

    public static void closeAll(Connection con, PreparedStatement pst, ResultSet rs)
    {
        try
        {
            if (con != null)
            {
                con.close();
            }
            if (pst != null)
            {
                pst.close();
            }
            if (rs != null)
            {
                rs.close();
            }
        }
        catch (SQLException e)
        {
            // TODO 自动生成 catch 块
            e.printStackTrace();
        }
    }

    public static int executeSQL(String sql, String[] parm)
    {
        Connection con = null;
        PreparedStatement pst = null;
        int num = 0;
        try
        {
            con = BaseDAO.getConn();
            pst = con.prepareStatement(sql);
            if (parm != null)
            {
                for (int i = 0; i < parm.length; i++)
                {
                    pst.setString(i + 1, parm[i]);
                }
            }
            num = pst.executeUpdate();
            // con.commit();
        }
        catch (SQLException e)
        {
            // con.rollback();
            // TODO Auto-generated catch block
            e.printStackTrace();

        }
        finally
        {
            BaseDAO.closeAll(con, pst, null);
        }
        return num;
    }
}
